using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Fund.Deferred;
using JlPay.Sdk.Models.Fund.Deferred;

namespace JlPay.Sdk.Examples.Fund.Deferred.FundFlowQuery
{
    /// <summary>
    /// 资金流水查询独立示例程序
    /// </summary>
    public class FundFlowQueryDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 资金流水查询独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await FundFlowQueryDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建资金流水查询请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static FundFlowQueryRequest BuildFundFlowQueryRequest()
        {
            var request = new FundFlowQueryRequest();
            
            // 设置请求参数
            request.MerchNo = "849551860110001";
            request.OutOrderId = "test_freeze_202412200001";
            
            return request;
        }

        /// <summary>
        /// 资金流水查询示例
        /// </summary>
        public static async Task FundFlowQueryDemoAsync()
        {
            Console.WriteLine("开始执行资金流水查询示例");
            
            try
            {
                // 创建配置对象和资金服务
                var config = CreateConfig();
                var fundService = new FundService(config);
                
                // 创建资金流水查询请求
                var request = BuildFundFlowQueryRequest();
                
                // 发送请求
                var response = await fundService.QueryFundFlowAsync(request);
                
                // 处理响应结果
                response.HandleResponse("资金流水查询", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new FundFlowQueryResponse();
                response.HandleResponseWithException("资金流水查询", config, ex);
            }
            
            Console.WriteLine("资金流水查询示例执行完毕");
        }
    }
} 